<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Summary_model extends My_Model
{
   
    public function __construct()
    {
        parent::__construct();
        $this->model_name="pyapl account report";
        $this->tablename="paypal_summary";   
    }
   
    
    public function summary($siteid,$accid,$amount)
    {
        $data=$this->get_item_by_where(["siteid"=>$siteid,"accid"=>$accid,"day"=>date("Y-m-d")]);
        if($data)
        {
            $newdata=[
                "succtotal"=>$data["succtotal"]+$amount,
                "succcount"=>$data["succcount"]+1,
            ];
            $this->update($data["id"],$newdata);
        }
        else
        {
            $insert=[
                "siteid"=>$siteid,"accid"=>$accid,"day"=>date("Y-m-d"),
                "succtotal"=>$amount,"succcount"=>1,
            ];
            $this->add_item($insert);
        }
    }

    public function getdetail($accountid,$timelimit)
    {
        $where="acccountid=".$accountid." and reportdate between '".$timelimit["start"]."' and '".$timelimit["end"]."'";
        $result=$this->get_items_by_where($where);
        return $result;
    }

    public function getrangelist($timelimit,$accid)
    {
        $sql="select siteid,sum(succcount) as totalcount,sum(succtotal) as totalamount from ".$this->table_prefix.$this->tablename." where accid={$accid} and day between '".$timelimit["start"]."' and '".$timelimit["end"]."'  group by siteid";
        $query=$this->db->query($sql);
        $res=$query->result_array();
        return $res;
    }
    
    
    public function getitembykey($val,$key="id")
    {
        $where=array($key=>$val);
        return $this->get_item_by_where($where);
    }

    public function getfilter($where)
    {
        return $this->get_items_by_where($where);
    }
    
    public function create($data){
    
        return $this->add_item($data);         
    }
    
    public function update($id,$data){
        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
          
        }else{
            $where=array("id"=>$id);
        
        }
        return $this->update_item($where,$data);
    }

   
}
